package com.sc.study.controller;

import com.sc.study.dto.ArticleQueryDTO;
import com.sc.study.entity.StudyArticle;
import com.sc.study.service.StudyArticleService;
import com.sc.study.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 研究文章控制器
 *
 * @author 徐赫阳
 * @date 2021/12/29
 */
@Api(description = "知识文章controller")
@RestController
@RequestMapping("/article")
public class StudyArticleController {
    /**
     * 服务对象
     */
    @Autowired
    private StudyArticleService studyArticleService;


    @ApiOperation("分页获取文章")
    @PostMapping("/getArticleList")
    public PageVO<StudyArticle> getArticleList(@RequestBody ArticleQueryDTO queryDTO){
        return studyArticleService.getArticleList(queryDTO);
    }



    @ApiOperation("通过id获取文章")
    @GetMapping("/getArticleById/{id}")
    public StudyArticle getArticleById(@PathVariable Integer id){
        return studyArticleService.getById(id);
    }

    @ApiOperation("保存文章")
    @PostMapping("/saveArticle")
    public Integer saveArticle(@RequestBody StudyArticle studyArticle, HttpServletRequest request) throws Exception{
        System.out.println(request.getSession(true).getAttribute("user"));
        if(studyArticle.getId() == null){
            studyArticle.setCreatorId((Integer)request.getSession(true).getAttribute("user"));
        }else{
            studyArticle.setUpdaterId((Integer)request.getSession(true).getAttribute("user"));
        }
        studyArticleService.saveOrUpdate(studyArticle);
        return studyArticle.getId();
    }

    @ApiOperation("删除")
    @DeleteMapping(value = "/deleteArticle/{id}")
    public void deleteArticle(@PathVariable Integer id){
        studyArticleService.removeById(id);
    }


}
